%% process one image
% It mainly read and convert image into suitable matrix.
function [res, succ] = deal_one(fullname, type, param)
succ = 0;
[~, ~, fext] = fileparts(fullname);
switch lower(param.space)
    case 'dct'
        if (param.use_jpegtbx == 1) && (strcmpi(fext,'.jpg') == 1)
            try
                jobj = jpeg_read(fullname);
                succ = 1;
            catch ME
                disp(ME);
            end
            if (succ ~= 0)
                res = feature_extractor(jobj.coef_arrays{1}, type, param);
            end
        else
            try
                img = imread(fullname);
                if size(img,3) == 3
                    img = rgb2gray(img);
                end
                img = double(img);
                succ = 1;
            catch ME
                disp(ME);
            end
            if (succ ~= 0)
                qtable = jpeg_qtable(param.qf);
                [M,N] = size(img);
                P = ceil(M / 8)*8;
                Q = ceil(N / 8)*8;
                pad = zeros(P, Q);% padding
                pad(1:M, 1:N) = img;
                fun = @(block_struct) dct2(block_struct.data) ./ qtable;% DCT
                coef = round(blockproc(pad - 128,[8 8], fun));
                res = feature_extractor(coef, type, param);
            end
        end
    case 'grey'
        try
            img = imread(fullname);
            if size(img,3) == 3
                img = rgb2gray(img);
            end
            succ = 1;
        catch ME
            disp(ME);
        end
        if (succ ~= 0)
            img = double(img);
            res = feature_extractor(img, type, param);
        end
    case 'color'
        try
            img = imread(fullname);
            succ = 1;
        catch ME
            disp(ME);
        end
        if (succ ~= 0)
            img = double(img);
            res = feature_extractor(img, type, param);
        end
    case 'path'
        res = feature_extractor(fullname, type, param);
        succ = 1;
    otherwise
        error('deal_one:space:UnknownSpace', 'Invalid value for space: %s.', param.space);
end
end %deal_one
